package com.swt.draw.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.swt.draw.dao.DepartmentMapper;
import com.swt.draw.entity.Admin;
import com.swt.draw.entity.Department;
import com.swt.draw.example.DepartmentExample;
import com.swt.draw.utils.DepartmentList2Tree;
import com.swt.draw.vo.BaseResponse;
import com.swt.draw.vo.TreetableResponseVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName
 * @Description TODO
 * @Author Administrator
 * @Date 2019/8/25 0025 10:36
 * @Version 1.0
 **/
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Resource
    private DepartmentMapper departmentMapper;

    /**
     * @Author ZPF
     * @Description 获取所有部门，以树形结构返回
     * @Date 10:38 2019/8/25 0025
     * @Param []
     * @return com.alibaba.fastjson.JSONArray
     **/
    @RequestMapping("/getAll")
    public List<Department> getDepartmentTree(){
        DepartmentExample example = new DepartmentExample();
        DepartmentExample.Criteria criteria = example.createCriteria();
        criteria.andDepartmentStatusNotEqualTo((short) 2);
        List<Department> departments = departmentMapper.selectByExample(example);
        return departments;
    }

    /**
     * @Author ZPF
     * @Description 获取机构树形表结构
     * @Date 22:46 2019/9/1 0001
     * @Param []
     * @return com.swt.draw.vo.TreetableResponseVO<com.swt.draw.entity.Department>
     **/
    @RequestMapping("/getDepTreetable")
    public TreetableResponseVO<Department> getDepartmentTreetable(){
        TreetableResponseVO responseVO = new TreetableResponseVO();
        DepartmentExample example = new DepartmentExample();
        DepartmentExample.Criteria criteria = example.createCriteria();
        criteria.andDepartmentStatusNotEqualTo((short) 2);
        List<Department> departments = departmentMapper.selectByExample(example);
        for(Department department : departments){
            if(department.getDepartmentParentNo() == null){
                department.setDepartmentParentNo("");
            }
        }
        Long count = departmentMapper.countByExample(example);
        responseVO.setCode(0);
        responseVO.setMsg("ok");
        responseVO.setCount(count);
        responseVO.setData(departments);
        return responseVO;
    }

    /**
     * @Author ZPF
     * @Description 添加部门
     * @Date 0:03 2019/9/2 0002
     * @Param [department]
     * @return com.swt.draw.vo.BaseResponse
     **/
    @PostMapping("/addDepartment")
    public BaseResponse addDepartment(Department department){
        BaseResponse response = new BaseResponse();
        if(department == null){
            return response;
        }
        if(department.getDepartmentStatus() == null){
            department.setDepartmentStatus((short) 1);
        }
        Admin admin = (Admin) SecurityUtils.getSubject().getPrincipal();
        String departmentNo = new StringBuffer("dep_").append(UUID.randomUUID()).toString();
        department.setDepartmentNo(departmentNo);
        department.setDepartmentCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        department.setDepartmentCreateBy(admin.getAdminNo());
        departmentMapper.insertSelective(department);
        response.setCode(1001);
        response.setDesc("success");
        return response;
    }

    /**
     * @Author ZPF
     * @Description 编辑部门
     * @Date 23:13 2019/9/2 0002
     * @Param [department]
     * @return com.swt.draw.vo.BaseResponse
     **/
    @PostMapping("/editDepartment")
    public BaseResponse editDepartment(Department department){
        BaseResponse response = new BaseResponse();
        if(department == null){
            return response;
        }
        if(department.getDepartmentStatus() == null){
            department.setDepartmentStatus((short) 1);
        }
        DepartmentExample example = new DepartmentExample();
        DepartmentExample.Criteria criteria = example.createCriteria();
        criteria.andDepartmentNoEqualTo(department.getDepartmentNo());
        departmentMapper.updateByExampleSelective(department, example);
        response.setCode(1001);
        response.setDesc("success");
        return response;
    }

    /**
     * @Author ZPF
     * @Description 删除某个部门机构及其所属机构
     * @Date 20:27 2019/9/2 0002
     * @Param [departmentNo]
     * @return com.swt.draw.vo.BaseResponse
     **/
    @PostMapping("/delAllChild")
    public BaseResponse delAllChild(String departmentNo){
        BaseResponse response = new BaseResponse();
        Integer rows = departmentMapper.delAllChild(departmentNo);
        response.setCode(1001);
        response.setDesc("success");
        response.setData(rows);
        return response;
    }
}
